<?php

namespace App\Model;

use PhalApi\Model\DataModel;
use PhalApi\Model\NotORMModel as NotORM;
use App\Model\IdcardInfo as Info;
use App\Model\IdcardFromUnitCheck as FromUnit;
use App\Model\CutDownTimes as CutDownTimes;
use App\Model\IdcardWater as IdcardWater;


class IdcardCheck extends DataModel
{
    public $tableName = 'idcard_check_list';
    protected function getTableName($id)
    {
        return $this->tableName;
    }

    //存入数据
    public function checkIdCard($data)
    {
        //读出采集人员信息
        $info = \PhalApi\DI()->notorm->phalapi_user->where('username', $data->checker)->fetchOne();
        $data->unit = $info['unit'];

        //单独对个人身份证建档
        $Md_info = new Info();
        $Md_info->checkIdCardInfo(
            array(
                "Name" => $data->Name,
                "IdNum" => $data->IdNum,
                "phone" =>  $data->phone,
                "filePath" => $data->filePath,
                "Sex" => $data->Sex,
                "Address" => $data->Address,
                "unit" => $data->unit,
            )
        );

        //按检测人对送检单位建档
        $Md_from_unit = new FromUnit();
        $Md_from_unit->check_from_unit(
            array(
                "checker" => $data->checker,
                "from_unit" => $data->from_unit,
                "phone" => $data->phone,
                "unit" => $data->unit
            )
        );

        //重构新数组
        $new_data = array();
        foreach ($data as $k => $v){
            if ($v){
                $new_data[$k]=$v;
            }
        }

        //插入更新时间
        $new_data['update_time'] = date("Y-m-d H:i:s", time());

        //重组数据
        $IdNum = $data->IdNum;
        $uid = $data->uid;
        //如果有uid单条结果
        if ( $uid ){
            $info = $this->getORM()->select('id')->where('uid', $uid)->fetchOne();
            //如果有信息,就返回相关数组, 并且更新
            if ($info) {
                $this->getORM()->where('id', $info['id'])->update($new_data);
                return '更新记录';
            }
        }

        //创建时间
        $new_data['create_time'] = date("Y-m-d H:i", time());
        //生成uid
        $md5 = md5($IdNum . $new_data['create_time']);
        $uid = array(
            substr($IdNum, 9,1),
            substr($IdNum, 11,1),
            substr($IdNum, 13,1),
            substr($md5, 0,2),
            substr($md5, 30,2),
            substr($IdNum, 0,2)
        );
        $uid = implode($uid);
        //生成的uid 合并到数据中
        $new_data['uid'] = $uid;

        //增加一个人数, 就扣一个点数
        if( $data->unit ){
            $CutDown = new CutDownTimes();
            $CutDown->cutDown($data->unit);
        }

        $IdcardWater = new IdcardWater();
        $new_data['water'] = $IdcardWater->Water($new_data);

        if( $new_data['from_unit_code'] ){
            $new_data['from_unit_code'] = $IdcardWater->Unit_Water($new_data);
        }
        

        //这里是钉钉提示的开始
        $curl = new \PhalApi\CUrl();
        // 设置请求的头部信息。
        $curl->setHeader(array('Content-Type' => 'application/json'));
        // 设置curl选项
        $postDate = array(
            "msgtype" => "text" ,
            "text" => array(
                "content" => $new_data['unit'] . "-" . $new_data['from_unit'] . "-" . $new_data['IdNum'] . "-" . $new_data['Name']
            )
        );
        $url = "https://oapi.dingtalk.com/robot/send?access_token=09d6f6a9ba294b1e5bf1452d73e47a00b213f8dc7b6991df7c8720b79ec8046c";
        $curl->post($url, json_encode($postDate),2000);
        //这里是钉钉提示的结束 - end


        //插入数据库
        $this->tableName = 'idcard_check_list';
        return $this->getORM()->insert($new_data);
    }
}
